package com.curry.apiprovider.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.curry.apiprovider.generator.mapper.TCityInfoMapper;
import com.curry.apiprovider.generator.mapper.TJobInfoMapper;
import com.curry.apiprovider.generator.mapper.TKeywordInfoMapper;
import com.curry.apiprovider.generator.service.impl.TJobInfoServiceImpl;
import com.curry.apiprovider.service.impl.RedisService;
import com.curry.livehelper.entity.domain.TJobInfo;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BaseOperatorService {
    private Logger logger = LoggerFactory.getLogger(BaseOperatorService.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private TKeywordInfoMapper tKeywordInfoMapper;
    @Autowired
    private TCityInfoMapper tCityInfoMapper;
    @Autowired
    private TJobInfoServiceImpl tJobInfoService;
    @Autowired
    private TJobInfoMapper tJobInfoMapper;

    private long EXPAIRETIME = 3600;

    /**
     * 查询
     *
     * @param cityName
     * @param keyWord
     * @param channelId
     * @param pageIndex
     * @return
     */
    public List<TJobInfo> readJobInfo(String cityName, String keyWord, String channelId, Integer pageIndex, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("cityName", cityName);
        params.put("channelId", channelId);
        String cityCode = tCityInfoMapper.getCityCodeByCityName(params);
        long startTime = System.currentTimeMillis();
        List<TJobInfo> info = redisService.readList(redisService.redisKey(keyWord, cityCode, Integer.valueOf(channelId),pageIndex,pageSize),TJobInfo.class);
        if (info != null && info.size()>0) {
            long endTime = System.currentTimeMillis();
            logger.warn("停止redis  " + (endTime - startTime));
            return info;
        } else {
            Map<String, Object> queryJobParam = new HashMap<>();
//            //查询数据库
            queryJobParam.put("keyWord", keyWord);
            queryJobParam.put("cityCode", cityCode);
            Map<String, Object> jobQueryParam = new HashMap<>();
            jobQueryParam.put("keyWord", keyWord);
            jobQueryParam.put("cityCode",cityCode);
            jobQueryParam.put("startIndex", pageIndex * pageSize);
            jobQueryParam.put("pageSize",pageSize);
            EntityWrapper<TJobInfo> entityWrapper = new EntityWrapper(new TJobInfo());
            entityWrapper.where("keyword={0}",keyWord)
                     .and("cityCode={0}");
            List<TJobInfo> tJobInfos  = tJobInfoMapper.selectPage(new RowBounds(pageIndex * pageSize,pageSize),entityWrapper);
//            List<TJobInfo> tJobInfos = tJobInfoMapper.findJobByPage(jobQueryParam);
            redisService.pushListItem(redisService.redisKey(keyWord, cityCode, Integer.valueOf(channelId),pageIndex,pageSize), tJobInfos, EXPAIRETIME);
            long endTime = System.currentTimeMillis();
            logger.warn("停止db " + (endTime - startTime));
            return tJobInfos;
        }
    }


}
